<?php
class Application_Form_Decorator_Edit_Common extends Zend_Form_Decorator_Abstract
{
	
	
	/**
	 *
	<dt id="username-label"><label for="username" class="optional">Username</label></dt>
	<dd id="username-element">
		<input type="text" name="username" id="username" value="" maxlength="256" />
		<p class="description">Please entere a valid email address.</p>
	</dd>
	 * @param string $content
	 */
	public function render ($content)
	{
		$element 			= $this->getElement();
		
		// If hidden element, just return element.
		if ($element instanceof Zend_Form_Element_Hidden) {
			return $content;
		}else if($element instanceof Application_Form_Element_Label){
			return $content;
		}else if($element instanceof Zend_Form_Element_Submit){
			return $content;
		}
		
		$labelTag 			= 'dt';
		$elementTag 		= 'dd';
		
		$requiredSuffix 	= $element->isRequired() ? ' <span class="form-required">*</span>' : '';
		$description		= $this->_buildDescription();
		$label				= $this->_buildLabel();
		$name				= $element->getName();
		
		return sprintf('<%s id="%s-element">%s</%s><%s>%s</%s>',
			$labelTag,
			$name,
			$label . $requiredSuffix,
			$labelTag,
			$elementTag,
			$content . $description,
			$elementTag
		);
	}
	
	protected function _buildLabel(){
		$element 		= $this->getElement();
		
		if ($element instanceof Zend_Form_Element_Submit) {
			// Buttons and submit buttons don't have a label.
			return '&nbsp;';
		}
		
		$label 			= $element->getLabel();
		
		return $label;
	}
	
	protected function _buildDescription(){
		$element 		= $this->getElement();
		$description 	= $element->getDescription();
		
		// display description above in separate row except for single checkbox.
		// display besides
//		if (strlen($description)>0) {
//			if ($element instanceof Zend_Form_Element_Checkbox) {
//				$description = sprintf(
//					'<span class="form-description">%s</span>',
//					$description
//				);
//			} else {
//				$description = sprintf(
//					'<dt>&nbsp;</dt><dd class="form-description">%s</dd>',
//					$description
//				);
//			}
//		}
		
		return '<p class="description">'.$description.'</p>';
	}
}